package alphaTab.platform.svg

import alphaTab.Logger
import alphaTab.alphaSkia.AlphaSkiaCanvas
import alphaTab.alphaSkia.AlphaSkiaPlatform
import alphaTab.alphaSkia.AlphaSkiaTextAlign
import alphaTab.alphaSkia.AlphaSkiaTextBaseline
import alphaTab.alphaSkia.AlphaSkiaTextStyle
import alphaTab.collections.DoubleList
import alphaTab.core.ecmaScript.Uint8Array
import kotlin.contracts.ExperimentalContracts

internal class FontSizesPartials {
    companion object {
        @ExperimentalUnsignedTypes
        @ExperimentalContracts
        public fun generateFontLookup(family: String) {
            if (FontSizes.fontSizeLookupTables.has(family)) {
                return
            }

            val measureSize = 11f

            val widths = DoubleList()
            val heights = DoubleList()

            try {
                if(AlphaSkiaPlatform.isNativeLibLoaded()) {
                    AlphaSkiaCanvas().use { canvas ->
                        canvas.beginRender(10, 10)

                        AlphaSkiaTextStyle(arrayOf(family), 400, false).use { style ->
                            for (i in (FontSizes.ControlChars.toInt() until 255)) {
                                val s = i.toChar().toString()
                                canvas.measureText(
                                    s,
                                    style,
                                    measureSize,
                                    AlphaSkiaTextAlign.LEFT,
                                    AlphaSkiaTextBaseline.ALPHABETIC
                                ).use { metrics ->
                                    widths.push(metrics.width.toDouble())
                                    val height =
                                        metrics.actualBoundingBoxDescent + metrics.actualBoundingBoxAscent
                                    heights.push(height.toDouble())
                                }
                            }
                        }

                        canvas.endRender().close()
                    }

                    FontSizes.fontSizeLookupTables.set(
                        family,
                        FontSizeDefinition(Uint8Array(widths), Uint8Array(heights))
                    )
                } else {
                    Logger.warning("Rendering", "Generating font lookup before alphaSkia init, SVG sizes will be wrong")
                    FontSizes.fontSizeLookupTables.set(family, FontSizeDefinition(Uint8Array(ubyteArrayOf((8).toUByte())), Uint8Array(ubyteArrayOf((10).toUByte()))))
                }
            } catch (e: Throwable) {
                Logger.error("Rendering", "Error while generating font lookup $e ${e.stackTraceToString()}")
                FontSizes.fontSizeLookupTables.set(family, FontSizeDefinition(Uint8Array(ubyteArrayOf((8).toUByte())), Uint8Array(ubyteArrayOf((10).toUByte()))))
            }
        }
    }
}
